//
//  shouYeMainViewController.m
//  yiYuanDuoBao
//
//  Created by 罗李苏 on 16/11/21.
//  Copyright © 2016年 罗李苏. All rights reserved.
//

#import "shouYeMainViewController.h"
#import "XRCarouselView.h"
#import "LS_CollectionReusableView.h"
#import "LS_shoyYeCollectionViewCell.h"
#import "LS_SearchProduceViewController.h"
#import "LS_MessageTableController.h"
//#import <AFNetworking.h>
//#import <UIImageView+WebCache.h>
#import "LS_getImageUrlModel.h"
@interface shouYeMainViewController ()<UICollectionViewDelegateFlowLayout,XRCarouselViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *control;
@property (nonatomic, strong) UIImageView *imageView;
//存放图片url的数组
@property (nonatomic, strong) NSArray *imageUrlArr;
//存放图片的数组
@property (nonatomic, strong) NSArray *imagesArr;
//定时器
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation shouYeMainViewController
//懒加载定时器
- (NSTimer *)timer{

    if (!_timer) {
        _timer= [[NSTimer alloc] init];
    }
    return _timer;
}
- (instancetype)init
{
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];

    self = [super initWithCollectionViewLayout:layout];
    if (self) {
        layout.minimumLineSpacing=0; //设置每一行的间距
        layout.minimumInteritemSpacing = 0;
        layout.itemSize=CGSizeMake(165, 265); //设置每个单元格的大小
        
        layout.sectionInset=UIEdgeInsetsMake(12, 12, 0, 12);
        
//        layout.headerReferenceSize=CGSizeMake(self.view.frame.size.width, 350); //设置collectionView头视图的大小
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"首页";
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 28, 28);
    //MARK:搜索按钮的点击事件
    [btn addTarget:self action:@selector(searchBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:[UIImage imageNamed:@"导航栏_搜索_icon"] forState:UIControlStateNormal];
    UIBarButtonItem *itemLeft = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = itemLeft;
    
//    [UIColor yellowColor];
    UIButton *btn1 = [[UIButton alloc]init];
    [btn1 addTarget:self action:@selector(messageBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    btn1.frame = CGRectMake(0, 0, 28, 28);
    [btn1 setBackgroundImage:[UIImage imageNamed:@"导航栏_消息_icon"] forState:UIControlStateNormal];
    UIBarButtonItem *btnRight = [[UIBarButtonItem alloc]initWithCustomView:btn1];
    self.navigationItem.rightBarButtonItem = btnRight;
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"LS_shoyYeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"shouYeCell"];
    //注册头部视图
    [self.collectionView registerClass:[LS_CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"headerView"];
}
- (void)searchBtnDidClick:(UIButton *)sender{

    NSLog(@"点击了搜索按钮");
//    [self.navigationController pushViewController:[LS_SearchProduceViewController new] animated:YES];
    [self presentViewController:[LS_SearchProduceViewController new] animated:YES completion:^{
        NSLog(@"真的跳了");
    }];
}
- (void)messageBtnDidClick:(UIButton *)sender{

    [self.navigationController pushViewController:[LS_MessageTableController new] animated:YES];
    NSLog(@"点击了消息提醒按钮");
}
//MARK:数据源方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    LS_shoyYeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"shouYeCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"LS_shoyYeCollectionViewCell" owner:nil options:nil] lastObject];
    }
    
    return cell;
}

//MARK:返回头部视图的方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    UICollectionReusableView *reusableView = nil;
    //判断如果是头部视图
    if (kind == UICollectionElementKindSectionHeader) {
        LS_CollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        headerView.lunView.delegate = self;
//        headerView.backgroundColor = [UIColor redColor];
        
//        for (LS_getImageUrlModel *model in headerView.imageUrlArr) {
//            
//            [headerView.imageView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"轮播图2：1"]];
//        }
        
        reusableView = headerView;
    }
     return reusableView;
    
}

#pragma mark 返回collectionView 头部实数的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(SCREEN_WIDTH,350);
}

#pragma mark XRCarouselViewDelegate
- (void)carouselView:(XRCarouselView *)carouselView clickImageAtIndex:(NSInteger)index {
    NSLog(@"点击了第%ld张图片", index);
}

////MARK:无限轮播图的方法
//- (void)loadTimer{
//
//    //设置定时器1秒调用1次,且不断重复
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(pageChange:) userInfo:nil repeats:YES];
//    //获取主循环
//    NSRunLoop *mainLoop = [NSRunLoop mainRunLoop];
//    //添加到运行循环(监听滚动)
//    [mainLoop addTimer:self.timer forMode:NSRunLoopCommonModes];
//    
//}
////当页码发生变化时调用
//- (void)pageChange:(id)sender{
//
//    //获取当前页的索引
//    NSInteger currentPage = self.control.currentPage;
//    //获取偏移量
//    CGPoint ofset = self.scrollView.contentOffset;
//    if (currentPage >= self.imagesArr.count - 1) {
//        //设置到首张图片
//        currentPage = 0;
//        //回复偏移量
//        ofset.x = 0;
//    }else{
//   
//        //当前索引加1
//        currentPage++;
//        //设置当前的偏移量
//        ofset.x += self.scrollView.bounds.size.width;
//    }
//    //设置当前页
//    self.control.currentPage = currentPage;
//    //设置偏移的位置
//    [self.scrollView setContentOffset:ofset animated:YES];
//}
////MARK:UIScrollView的代理方法
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//
//    //正在滚动
//    NSInteger currentPage = round(scrollView.contentOffset.x / self.collectionView.bounds.size.width);
//    self.control.currentPage = currentPage;
//}
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//
//    //开始拖拽的时候 停止定时器
//    [self.timer invalidate];
//}
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//
//    //拖拽结束后开始自动轮播
//    [self loadTimer];
//}
@end
